<script type="text/javascript">
    $(document).ready(function() {
        $("#tabsrapport").tytabs({
            prefixtabs:"tabr",
            prefixcontent:"contentr",
            classcontent:"tabscontent",
            tabinit:"1",
            fadespeed:"normal"
        });
        
        $('.qte3').show();
        
        // Toggle rapport panel
        $.fn.toggleotherpanels = function(){
            return this.each(function(){
                $(this).addClass("rapportaccordion")
                    .find("p.rapport-section")
                    //.addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom")
                    .addClass("rapportaccordion-header rapportaccordion-state-default")
                    .hover(function() { $(this).toggleClass("rapportaccordion-state-hover"); })
                    //.prepend('<span class="ui-icon ui-icon-triangle-1-e"></span>')
                    .click(function() {
                        $(this)
                            //.toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom")
                            .toggleClass("rapportaccordion-header-active rapportaccordion-state-active rapportaccordion-state-default")
                            //.find("> .ui-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s").end()
                            .next().slideToggle();
                        return false;
                    })
                    .next()
                    .addClass("rapportaccordion-content")
                    .hide();
            });
        };
        $(".rapportpanel").toggleotherpanels();
    });
    
    function toggleSemaine(el) {
        var elId = $(el).attr('id');
        if ($(el).hasClass('plus')) {
            $(el).removeClass('plus');
            $(el).addClass('moins');            
            $('.'+elId).fadeIn(1000);
        } else if ($(el).hasClass('moins')) {
            $(el).removeClass('moins');
            $(el).addClass('plus');
            $('.'+elId).fadeOut(500);
        }
    }
</script>
<?php
//var_dump($rptProduit);
$qteMonth = $qteWeek = $qteDay = array();
$cattcMonth = $cattcWeek = $cattcDay = array();
$cahtMonth = $cahtWeek = $cahtDay = array();
foreach ($period as $idxm => $month) { 
    // Report for month
    foreach($rptProduit as $prd) {
        $explodeOrderDate = explode('-',$prd['orderDate']);
        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];  

        if($thisMonth == $idxm) {
            @$qteMonth[$idxm][$prd['alibelle']] += $prd['quantite'];
            @$cattcMonth[$idxm][$prd['alibelle']] += $prd['cattc'];
            @$cahtMonth[$idxm][$prd['alibelle']] += $prd['caht'];
        }
    }
    
    // Report for week
    foreach ($month as $idxw => $week) {
        foreach($rptProduit as $prd) {
            $orderDate = new \DateTime($prd['orderDate']);            
            $orderDateWeek = $orderDate->format('W');

            $explodeOrderDate = explode('-',$prd['orderDate']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];  

            if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                @$qteWeek[$idxw][$prd['alibelle']] += $prd['quantite'];
                @$cattcWeek[$idxw][$prd['alibelle']] += $prd['cattc'];
                @$cahtWeek[$idxw][$prd['alibelle']] += $prd['caht'];
            }
        }
        
        // Report for day
        foreach ($week as $idxd => $day) {
            foreach($rptProduit as $prd) {
                if($prd['orderDate'] == $day->format('Y-m-d') && $prd['canceled'] == 0) {
                    @$qteDay[$prd['orderDate']][$prd['alibelle']] += $prd['quantite'];
                    @$cattcDay[$prd['orderDate']][$prd['alibelle']] += $prd['cattc'];
                    @$cahtDay[$prd['orderDate']][$prd['alibelle']] += $prd['caht'];
                }
            }
        }
    }
} 
//var_dump(@$cattcDay);
?>

<div id="tabsrapport">
    <ul class="tabs">
    <?php 
    $x = 1; 
    foreach ($period as $idxm => $month) { ?>
        <li id="tabr<?php echo $x; ?>"><?php echo $idxm; ?></li>
    <?php $x++; } ?>
    </ul>
    
    <?php 
    $y = 1; 
    foreach ($period as $idxm => $month) {        
    ?>
    <div class="contents">
        <div id="contentr<?php echo $y; ?>" class="tabscontent">
            <table>
                <tr>
                    <td style="width: 150px;"><div style="width: 150px;"></div></td>
                    <td class="tabs-rapport-top">
                        <table>
                            <tr>
                                <th class="width175">
                                    <table class="width175">
                                        <tr class="haut2">
                                            <td colspan="4"><p><?php echo $idxm; ?></p></td>
                                        </tr>
                                        <tr class="bas">
                                            <td width="25%">V</td>
                                            <td width="25%">P</td>
                                            <td width="25%">O</td>
                                            <td width="25%">R</td>
                                        </tr>
                                    </table>
                                </th>
                                <?php $z=1; foreach ($month as $idxw => $week) { ?>
                                <th class="width175">
                                    <table class="width175">
                                        <tr class="haut">
                                            <td colspan="4"><p>Semaine <?php echo $z; ?>
                                                    <span id="rps<?php echo $idxw; ?>" class="toogle plus" onclick="toggleSemaine(this);"></span></p></td>
                                        </tr>
                                        <tr class="bas">
                                            <td width="25%">V</td>
                                            <td width="25%">P</td>
                                            <td width="25%">O</td>
                                            <td width="25%">R</td>
                                        </tr>
                                    </table>
                                </th>
                                
                                <?php foreach ($week as $idxd => $day) { ?>
                                <th class="width175 hide rps<?php echo $idxw; ?>">
                                    <table class="width175">
                                        <tr class="haut">
                                            <td colspan="4"><p class="blackcolor"><?php echo $idxd.'('.$day->format('d/m/Y').')'; ?></p></td>
                                        </tr>
                                        <tr class="bas">
                                            <td width="25%">V</td>
                                            <td width="25%">P</td>
                                            <td width="25%">O</td>
                                            <td width="25%">R</td>
                                        </tr>
                                    </table>
                                </th>
                                <?php } ?>
                                
                                <?php $z++; } ?>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <?php $lastfname = ""; $lastalibelle = "";
                        $count = count($rptProduit);
                        for($x=0;$x<$count;$x++) { 
                            $alibelle = $rptProduit[$x]['alibelle'];
                            $fname = $rptProduit[$x]['fname'];
                            $nextfname = @$rptProduit[$x+1]['fname'];
                            
                            if($fname != $lastfname) { ?>
                            <div class="rapportpanel">
                                <p class="rapport-section"><span class="icons"></span><span class="text"><?php echo $fname; ?></span></p>
                                <div>
                                    <table> 
                            <?php } ?>
                                        <?php if($alibelle != $lastalibelle){ ?>
                                        <?php 
                                        $explodeOrderDate2 = explode('-',$rptProduit[$x]['orderDate']);
                                        $thisMonth2 = $monthText[intval($explodeOrderDate2[1])]." ".$explodeOrderDate2[0]; 
                                        if($thisMonth2 == $idxm) {
                                        ?>
                                        <tr>
                                            <td class="tabs-rapport-left"><?php echo $rptProduit[$x]['alibelle']; ?></td>
                                            <td class="tabs-rapport-right">
                                                <table>
                                                    <tr>
                                                        <td class="width175">
                                                            <table class="width175">
                                                                <tr class="detailValue qte3">
                                                                    <td width="25%"><?php echo $qteMonth[$idxm][$alibelle]; ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                                <tr class="detailValue cattc3">
                                                                    <td width="25%"><?php echo number_format($cattcMonth[$idxm][$alibelle],2,',', ''); ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                                <tr class="detailValue caht3">
                                                                    <td width="25%"><?php echo number_format($cahtMonth[$idxm][$alibelle],2,',', ''); ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                        <?php $zz=1; foreach ($month as $idxw => $week) { ?>
                                                        <td class="width175">
                                                            <table class="width175">
                                                                <tr class="detailValue qte3">
                                                                    <td width="25%"><?php echo isset($qteWeek[$idxw][$alibelle])?$qteWeek[$idxw][$alibelle]:0; ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                                <tr class="detailValue cattc3">
                                                                    <td width="25%"><?php echo number_format(isset($cattcWeek[$idxw][$alibelle])?$cattcWeek[$idxw][$alibelle]:0,2,',', ''); ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                                <tr class="detailValue caht3">
                                                                    <td width="25%"><?php echo number_format(isset($cahtWeek[$idxw][$alibelle])?$cahtWeek[$idxw][$alibelle]:0,2,',', ''); ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                            </table>
                                                        </td>

                                                        <?php foreach ($week as $idxd => $day) { ?>
                                                        <td class="width175 hide rps<?php echo $idxw; ?>">
                                                            <table class="width175">
                                                                <tr class="detailValue qte3">
                                                                    <td width="25%"><?php echo isset($qteDay[$day->format('Y-m-d')][$alibelle])?$qteDay[$day->format('Y-m-d')][$alibelle]:0; ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                                <tr class="detailValue cattc3">
                                                                    <td width="25%"><?php echo number_format(isset($cattcDay[$day->format('Y-m-d')][$alibelle])?$cattcDay[$day->format('Y-m-d')][$alibelle]:0,2,',', ''); ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                                <tr class="detailValue caht3">
                                                                    <td width="25%"><?php echo number_format(isset($cahtDay[$day->format('Y-m-d')][$alibelle])?$cahtDay[$day->format('Y-m-d')][$alibelle]:0,2,',', ''); ?></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                    <td width="25%"></td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                        <?php } ?>

                                                        <?php $zz++; } ?>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                        <?php                                         
                                        } }
                                        $lastfname = $fname;
                                        $lastalibelle = $alibelle;
                                        if($fname != $nextfname) { 
                                        ?>
                                    </table>
                                </div>
                            </div>
                        <?php 
                                        }
                        }                         
                        ?>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <?php $y++; } ?>
</div>

